package main.kotlin.solution

class GetLeastNumber {
    fun getLeastNumbers(arr: IntArray, k: Int): IntArray {
        if(k==0){
            return IntArray(0)
        }

        var resultArray = IntArray(k)

        for(i in 0 until k){
            resultArray[i] = arr[i]
        }

        var index = k
        for(i in k until arr.size){
            doDataInsert(arr[i],resultArray)
        }
        return resultArray
    }

    fun doDataInsert(value:Int,array:IntArray):IntArray{
        var maxValueIndex = 0
        for(index in array.indices){
            if(array[index] > array[maxValueIndex]){
                maxValueIndex = index
            }
        }

        if(value < array[maxValueIndex]){
            array[maxValueIndex] = value
        }
        return array
    }
}